import {assert} from 'chai'
import * as request from 'superagent/lib/client'
import {createXHRClass} from "../WXXHR";
import RequestOptions = wx.RequestOptions;


let successMockResponse: any = {
  statusCode: 200, data: {ok: true}, header: {
    'Content-Type': 'application/json'
  }
}

let failMockResponse: any = {
  statusCode: 400, data: {ok: true}, header: {
    'Content-Type': 'application/json'
  }
}


const mockRequest: (option: RequestOptions) => void = ({
                                                         url
                                                         , method, success, header, fail
                                                       }) => {

  if (successMockResponse) {
    success(successMockResponse)
    return
  }

  if (failMockResponse) {
    fail()
  }
}

const WXXHR = createXHRClass({

  request: mockRequest
})


request.getXHR = function () {
  return new WXXHR()
}


describe('wechatapp-xhr', () => {


  it('POST', (done) => {

    successMockResponse = {
      statusCode: 200, data: {post: true}, header: {
        'Content-Type': 'application/json'
      }
    }

    request.get('http://test.com')
      .send({test: 'post'})
      .end((err, res) => {
        done(err)
      })

  });

  it('get', (done) => {

    request.get('http://test.com')
      .query({q1: 'test1', q2: 42})
      .end((err, res) => {
        done(err)

        console.log(res);
        console.log(res.body);
      })

  })

  it('fail', (done) => {
    successMockResponse = null
    failMockResponse = {fail: 'oops'}
    request.get('http://test.com')
      .query({q1: 'test1', q2: 42})
      .end((err, res) => {
        done(!err)
      })


  })

})
